package com.example.ex5_springboot.controller;

import com.example.ex5_springboot.Result;
import com.example.ex5_springboot.pojo.AdminLogin;
import com.example.ex5_springboot.pojo.UserLogin;
import com.example.ex5_springboot.services.AdminLoginService;
import com.example.ex5_springboot.services.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(allowedHeaders = "*")
public class UserLoginController {
    @Autowired
    private UserLoginService userLoginService;

    @PostMapping("/loginUser")
    public Result login(@RequestParam("userId")String id,
                        @RequestParam("password")String pwd){
        Integer userId = 0;
        try {
            userId = Integer.parseInt(id);
        } catch (NumberFormatException e){
            return Result.fail();
        } catch (Exception e) {
            return Result.fail();
        }

        UserLogin userLogin = userLoginService.getUserInfoById(userId);
        if (userLogin == null){
            return Result.fail();
        }  else if (userLogin.getEnable() == 0){
            String message = "您的账号被锁定！";
            return Result.success(message);
        } else if (userLogin.getUser_pwd().equals(pwd)){
            return Result.success(userLogin);
        } else {
            return Result.fail();
        }
    }

    @GetMapping("/getUserChange")
    public Result getUserChange(){
        try {
            List<UserLogin> userLoginList = userLoginService.getUserInfo();
            return Result.success(userLoginList);
        } catch (Exception e){
            return Result.fail();
        }
    }

    @PostMapping("/changeEnable")
    public Result changeEnable(@RequestParam("id")String Id,
                                @RequestParam("enable")String Enable){
        Integer enable = 0;
        Integer id = 0;
        try {
            enable = Integer.parseInt(Enable);
            id = Integer.parseInt(Id);
        } catch (NumberFormatException e) {
            return  Result.fail();
        } catch (Exception e) {
            return  Result.fail();
        }
        int i = userLoginService.changeEnable(enable,id);
        if (i != 0) {
            return Result.success(i);
        } else {
            return Result.fail();
        }
    }

    @PostMapping("/editUserChangeInfo")
    public Result editUserChangeInfo(@RequestParam("id")String Id,
                                     @RequestParam("college")String college,
                                     @RequestParam("name")String name){
        Integer id = 0;
        try {
            id = Integer.parseInt(Id);
        } catch (NumberFormatException e) {
            return  Result.fail();
        } catch (Exception e) {
            return  Result.fail();
        }
        int i = userLoginService.editUserChangeInfo(id,college,name);
        if (i != 0) {
            return Result.success(i);
        } else {
            return Result.fail();
        }
    }

    @PostMapping("/insertUser")
    public Result insertUser(@RequestParam("id")String Id,
                             @RequestParam("college")String college,
                             @RequestParam("name")String name){
        Integer id = 0;
        try {
            id = Integer.parseInt(Id);
        } catch (NumberFormatException e) {
            return  Result.fail();
        } catch (Exception e) {
            return  Result.fail();
        }
        int i = userLoginService.insertUser(id,college,name);
        if (i != 0) {
            return Result.success(i);
        } else {
            return Result.fail();
        }
    }

    @PostMapping("/updateUser")
    public Result updateUser(@RequestParam("id")String Id,
                             @RequestParam("petName")String petName,
                             @RequestParam("pwd")String pwd){
        Integer id = 0;
        try {
            id = Integer.parseInt(Id);
        } catch (NumberFormatException e) {
            return  Result.fail();
        } catch (Exception e) {
            return  Result.fail();
        }
        int i = userLoginService.updateUser(id,petName,pwd);
        if (i != 0) {
            return Result.success(i);
        } else {
            return Result.fail();
        }
    }
}
